<template>
  <div>
    <BasicTable @register="registerTable" :loading="true">
      <!-- 标记 -->
      <template #statusFlag="{ record }">
        <template v-if="record.flag == 0">
          <Tag :color="'green'">启用</Tag>
        </template>
        <template v-if="record.flag == 5">
          <Tag :color="'yellow'">禁用</Tag>
        </template>
        <template v-if="record.flag == 9">
          <Tag :color="'red'">已删除</Tag>
        </template>
      </template>

      <template #toolbar>
        <a-button type="primary" @click="onAddRole"> 新增 </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditRole.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: onDeleteRole.bind(null, record),
              },
            },
            {
              icon: 'ant-design:audit-outlined',
              label: '关联用户',
              onClick: onSetUsers.bind(null, record),
            },
            {
              icon: 'ant-design:audit-outlined',
              label: '关联菜单',
              color: 'success',
              onClick: onSetMenus.bind(null, record),
            },
            {
              icon: 'ant-design:audit-outlined',
              label: '关联部门',
              color: 'warning',
              onClick: onSetDepts.bind(null, record),
            },
            {
              icon: 'ant-design:share-alt-outlined',
              label: '关联Api',
              onClick: onSetApi.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>

    <addOrUpdateRole @register="registerAddOrUpdateForm" @success="afterSuccess" />
    <deptTreeModel @register="registerDeptTree" @success="afterSuccess" />
    <menuTreeModel @register="registerMenuTree" @success="afterSuccess" />
    <deptUserTreeModel @register="registerDeptUserTree" @success="afterSuccess" />
    <apiTreeModal @register="registerApiTree" @success="afterSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { queryRoles, deleteRole } from '/@/api/project/role';
  import { roleTableColumns, queryConditionFormSchema } from './role.data';

  import { Tag } from 'ant-design-vue';

  //引入子组件
  import addOrUpdateRole from './AddOrUpdateRole.vue';
  import deptUserTreeModel from './DeptUserTreeModel.vue';
  import menuTreeModel from './MenuTreeModel.vue';
  import deptTreeModel from './DeptTreeModel.vue';
  import apiTreeModal from './ApiTreeModal.vue';

  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';
  const { createMessage } = useMessage();

  const [registerAddOrUpdateForm, { openModal: openAddOrUpdateForm }] = useModal();
  const [registerDeptUserTree, { openModal: openDeptUserTree }] = useModal();
  const [registerMenuTree, { openModal: openMenuTree }] = useModal();
  const [registerDeptTree, { openModal: openDeptTree }] = useModal();
  const [registerApiTree, { openModal: openApiTree }] = useModal();

  export default defineComponent({
    name: 'RoleManagement',
    components: {
      BasicTable,
      TableAction,
      Tag,
      addOrUpdateRole,
      deptUserTreeModel,
      menuTreeModel,
      deptTreeModel,
      apiTreeModal,
    },
    setup() {
      /**
       * 请求后台查询接口返回数据显示
       */
      const [registerTable, { reload }] = useTable({
        title: '角色管理',
        api: queryRoles,
        columns: roleTableColumns, //表头数据
        formConfig: {
          labelWidth: 80, //设置查询表单中label的宽度
          schemas: queryConditionFormSchema, // form查询表单
          showResetButton: false, //查询条件部分中是否显示重置按钮
        },
        useSearchForm: true,
        showTableSetting: true, //是否显示表格设置
        showIndexColumn: true,
        canResize: true,
        bordered: true,
        striped: false,
        actionColumn: {
          width: 300,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
        pagination: false, //关闭分页
      });

      /**
       * 打开新增窗口
       */
      function onAddRole() {
        //使用props传递数据至子组件，在data参数中指定
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '角色管理',
          operateType: OperationTypeEnum.ADD,
        };
        openAddOrUpdateForm(true, propsData);
      }

      /**
       * 打开编辑窗口
       */
      function onEditRole(record: Recordable) {
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '角色管理',
          operateType: OperationTypeEnum.EDIT,
          dataId: record.id,
        };
        openAddOrUpdateForm(true, propsData);
      }

      /**
       * 打开设置角色关联用户弹窗
       */
      function onSetUsers(record: Recordable) {
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '角色管理',
          operateString: '设置角色关联用户',
          dataId: record.id,
        };
        openDeptUserTree(true, propsData);
      }

      /**
       * 打开设置角色关联菜单窗口
       */
      function onSetMenus(record: Recordable) {
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '角色管理',
          operateString: '设置角色关联菜单',
          dataId: record.id,
        };
        openMenuTree(true, propsData);
      }

      /**
       * 打开设置角色关联部门窗口
       */
      function onSetDepts(record: Recordable) {
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '角色管理',
          operateString: '设置角色关联部门',
          dataId: record.id,
        };
        openDeptTree(true, propsData);
      }

      /**
       * 打开设置角色关联Api窗口
       */
      function onSetApi(record: Recordable) {
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '角色管理',
          operateString: '设置角色关联Api',
          dataId: record.id,
        };
        openApiTree(true, propsData);
      }

      /**
       * 删除
       */
      function onDeleteRole(record: Recordable) {
        deleteRole(record.id)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }

      return {
        registerTable,
        afterSuccess,
        registerAddOrUpdateForm,
        registerDeptUserTree,
        registerApiTree,

        onAddRole,
        onEditRole,
        onSetUsers,
        onDeleteRole,
        onSetMenus,
        onSetApi,

        registerDeptTree,
        registerMenuTree,
        onSetDepts,
      };
    },
  });
</script>
